﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace AlarmManagePlatform.DataAccess
{
    internal class tb_DictInfo
    {
        internal List<Model.DB.tb_DictInfo> getForDictType(string DictType)
        {
            Relations.DBUtility.SqlServer.SqlDb sqlDb = null;
            List<Model.DB.tb_DictInfo> result = new List<Model.DB.tb_DictInfo>();
            Model.DB.tb_DictInfo tb_DictInfo;
            try
            {
                DataTable dataTable;
                string sqlScript;
                sqlScript = string.Format("Select Id,DictType,DictCode,DictValue,DictDesc,OrderId From tb_DictInfo where DictType = '{0}';", DictType);
                sqlDb = Static.Methods.OpenDataBase();
                dataTable = sqlDb.RunSqlScript(sqlScript);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    tb_DictInfo = new Model.DB.tb_DictInfo();
                    tb_DictInfo.Id = int.Parse(dataTable.Rows[i]["Id"].ToString());
                    tb_DictInfo.DictType = dataTable.Rows[i]["DictType"].ToString();
                    tb_DictInfo.DictCode = dataTable.Rows[i]["DictCode"].ToString();
                    tb_DictInfo.DictValue = dataTable.Rows[i]["DictValue"].ToString();
                    tb_DictInfo.DictDesc = dataTable.Rows[i]["DictDesc"].ToString();
                    tb_DictInfo.OrderId = int.Parse(dataTable.Rows[i]["OrderId"].ToString());
                    result.Add(tb_DictInfo);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取数据字典信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (sqlDb != null)
                    Static.Methods.CloseDataBase(sqlDb);
            }
        }

        internal List<Model.DB.tb_DictInfo> get(string DictType, string DictCode)
        {
            Relations.DBUtility.SqlServer.SqlDb sqlDb = null;
            List<Model.DB.tb_DictInfo> result = new List<Model.DB.tb_DictInfo>();
            Model.DB.tb_DictInfo tb_DictInfo;
            try
            {
                DataTable dataTable;
                string sqlScript;
                sqlScript = string.Format("Select Id,DictType,DictCode,DictValue,DictDesc,OrderId From tb_DictInfo where DictType = '{0}' and DictCode = '{1}';", DictType, DictCode);
                sqlDb = Static.Methods.OpenDataBase();
                dataTable = sqlDb.RunSqlScript(sqlScript);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    tb_DictInfo = new Model.DB.tb_DictInfo();
                    tb_DictInfo.Id = int.Parse(dataTable.Rows[i]["Id"].ToString());
                    tb_DictInfo.DictType = dataTable.Rows[i]["DictType"].ToString();
                    tb_DictInfo.DictCode = dataTable.Rows[i]["DictCode"].ToString();
                    tb_DictInfo.DictValue = dataTable.Rows[i]["DictValue"].ToString();
                    tb_DictInfo.DictDesc = dataTable.Rows[i]["DictDesc"].ToString();
                    tb_DictInfo.OrderId = int.Parse(dataTable.Rows[i]["OrderId"].ToString());
                }
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取数据字典信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (sqlDb != null)
                    Static.Methods.CloseDataBase(sqlDb);
            }
        }


        internal List<Model.DB.tb_DictInfo> GetDictInfoList(string DictType, string DictCode)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            List<Model.DB.tb_DictInfo> _Result;
            DataTable _DataTable;
            string _SqlScript;
            try
            {

                _SqlScript = string.Format("Select Id,DictType,DictCode,DictValue,DictDesc,OrderId From tb_DictInfo where DictType = '{0}' and DictCode = '{1}';", DictType, DictCode);
                _SqlDb = Static.Methods.OpenDataBase();
                _DataTable = _SqlDb.RunSqlScript(_SqlScript);

                _Result = DataTableToList(_DataTable);

                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取数据字典信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        private List<Model.DB.tb_DictInfo> DataTableToList(DataTable dataTable)
        {
            List<Model.DB.tb_DictInfo> _Result;
            Model.DB.tb_DictInfo _Temp;
            try
            {
                _Result = new List<Model.DB.tb_DictInfo>();
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    _Temp = new Model.DB.tb_DictInfo();
                    _Temp.Id = int.Parse(dataTable.Rows[i]["Id"].ToString());
                    _Temp.DictType = dataTable.Rows[i]["DictType"].ToString();
                    _Temp.DictCode = dataTable.Rows[i]["DictCode"].ToString();
                    _Temp.DictValue = dataTable.Rows[i]["DictValue"].ToString();
                    _Temp.DictDesc = dataTable.Rows[i]["DictDesc"].ToString();
                    _Temp.OrderId = int.Parse(dataTable.Rows[i]["OrderId"].ToString());
                    _Result.Add(_Temp);
                }
                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("将Datatable转换成List<Model.DB.tb_DictInfo>有异常：" + ex.ToString());
            }
        }

        public void UpdateDictValue(string dictType, string dictCode, string dictValue)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            string _SqlScript = string.Empty;
            try
            {
                _SqlDb = Static.Methods.OpenDataBase();

                _SqlScript = $"Update tb_DictInfo Set DictValue = '{dictValue}'";
                _SqlScript += $" Where DictType = '{dictType}' And  DictCode = '{dictCode}'";

                _SqlDb.UpdateSqlScript(_SqlScript);
            }
            catch (Exception ex)
            {
                throw new Exception("更新数据字典数据值失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }
    }
}